package com.nsoft.remotemedia;

import java.util.List;

import com.nsoft.remotemedia.business.YoutubeLoadListener;
import com.nsoft.remotemedia.business.YoutubeLoader;
import com.nsoft.remotemedia.objects.VideoItem;
import com.nsoft.remotemedia.utils.Constant;
import com.nsoft.remotemedia.utils.MostPopularListAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class U2bActivity extends Activity 
					implements YoutubeLoadListener{
	private ListView feedList;
	private EditText searchTxt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_u2b);
		
		addControls();
		loadNewFeeds();
	}

	@Override
	public void updateU2bFeeds(List<VideoItem> list) {
		MostPopularListAdapter adapter=
				new MostPopularListAdapter(this,list);
		feedList.setAdapter(adapter);
	}

	private void addControls(){
		searchTxt=(EditText)findViewById(R.id.editU2bSearch);
		searchTxt.setOnEditorActionListener(new 
				TextView.OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v,
							int actionId,KeyEvent event) {
				if (actionId==EditorInfo.IME_ACTION_SEARCH) {
					doSearch();
		            return true;
		        }
		        return false;

			}
		});
		
		
		ImageButton searchBtn=(ImageButton)
					findViewById(R.id.buttonSearch);
		searchBtn.setOnClickListener(
					new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doSearch();
			}
		});
		
		feedList=(ListView)findViewById(R.id.listItems);
		
	}
	
	private void doSearch(){
		String searchStr=searchTxt.getText().toString();
		YoutubeLoader u2bLoader=new 
				YoutubeLoader(this,searchStr);
		u2bLoader.setLoadListener(this);
		u2bLoader.execute();
	}
	
	private void loadNewFeeds(){
		YoutubeLoader u2bLoader=new YoutubeLoader(
					this,Constant.EMPTY);
		u2bLoader.setLoadListener(this);
		u2bLoader.execute();
	}

}
